package dws_api

import (
	"gorm.io/gorm"
	"server/apps/ods/ods_field"
	"server/apps/ods/ods_table"
	"server/global"
	"server/model/common/request"
)

type DwsApiService struct {
}


func (dwsApiService *DwsApiService) CreateDwsApi(dwsApi *DwsApi) (err error) {
	err = global.SysDB.Create(dwsApi).Error
	return err
}


func (dwsApiService *DwsApiService) DeleteDwsApi(dwsApi DwsApi) (err error) {
	err = global.SysDB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&DwsApi{}).Where("id = ?", dwsApi.ID).Update("deleted_by", dwsApi.DeletedBy).Error; err != nil {
			return err
		}
		if err = tx.Delete(&dwsApi).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}


func (dwsApiService *DwsApiService) DeleteDwsApiByIds(ids request.IdsReq, deleted_by uint) (err error) {
	err = global.SysDB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&DwsApi{}).Where("id in ?", ids.Ids).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", ids.Ids).Delete(&DwsApi{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}


func (dwsApiService *DwsApiService) UpdateDwsApi(dwsApi DwsApi) (err error) {
	err = global.SysDB.Save(&dwsApi).Error
	return err
}


func (dwsApiService *DwsApiService) GetDwsApiById(id uint) (dwsApi DwsApi, err error) {
	err = global.SysDB.Where("id = ?", id).First(&dwsApi).Error
	return
}


func (dwsApiService *DwsApiService) GetDwsApiInfoList(info DwsApiSearch) (list []DwsApi, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&DwsApi{})
	var dwsApis []DwsApi
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&dwsApis).Error
	return dwsApis, total, err
}


func (dwsApiService *DwsApiService) GetAllDwsApis() (dwsApi []DwsApi, err error) {
	err = global.SysDB.Find(&dwsApi).Error
	return dwsApi, err
}


func (dwsApiService *DwsApiService) GetDwsApi(id uint) (apiConfig ApiConfig, err error) {
	var eApiConfig ApiConfig
	apiConfig.DwsApi, err = dwsApiService.GetDwsApiById(id)
	if err != nil {
		return eApiConfig, err
	}
	apiConfig.DwsTbs, err = dwsTbService.GetDwsTbByCol("apiId", id)
	if err != nil {
		return eApiConfig, err
	}
	for idx, oneDwsTb := range apiConfig.DwsTbs {
		var tbCount int64
		global.SysDB.Find(&[]ods_table.OdsDbTable{}, "odsTbName = ? and bsm = ? and id=?", oneDwsTb.SrcTbName, oneDwsTb.SrcTbBsm, oneDwsTb.SrcTbId).Count(&tbCount)
		if tbCount > 0 {
			apiConfig.DwsTbs[idx].SrcTbExist = 1
		} else {
			apiConfig.DwsTbs[idx].SrcTbExist = 0
		}
		if tbCount > 0 {
			err = global.SysDB.Where("odsTbId = ?", oneDwsTb.SrcTbId).Find(&apiConfig.DwsTbs[idx].SrcFlds).Error
			for _, oneRow := range apiConfig.DwsTbs[idx].SrcFlds {
				apiConfig.DwsTbs[idx].ColList = append(apiConfig.DwsTbs[idx].ColList, oneRow.ColName)
			}
		}
	}

	err = global.SysDB.Where("apiId = ?", id).Find(&apiConfig.DwsFlds).Error
	if err != nil {
		return
	}

	var dwsTbFlds []ods_field.OdsDbField
	for _, oneDwsTb := range apiConfig.DwsTbs {
		dwsTbFlds = append(dwsTbFlds, oneDwsTb.SrcFlds...)
	}
	for _, tFld := range dwsTbFlds {
		for idx, fFld := range apiConfig.DwsFlds {
			if fFld.SrcFldId == tFld.ID {
				apiConfig.DwsFlds[idx].SrcFldExist = 1
			}
		}
	}

	return apiConfig, nil
}
